package com.qf.studentmanager.servlet;

import com.fasterxml.jackson.databind.ObjectMapper;
import com.qf.studentmanager.model.RespBean;
import com.qf.studentmanager.model.RespPageBean;
import com.qf.studentmanager.model.Teacher;
import com.qf.studentmanager.service.TeacherService;

import javax.servlet.ServletException;
import javax.servlet.annotation.WebServlet;
import javax.servlet.http.HttpServlet;
import javax.servlet.http.HttpServletRequest;
import javax.servlet.http.HttpServletResponse;
import java.io.IOException;
import java.util.List;

@WebServlet(urlPatterns = "/Teacher")
public class TeacherServlet extends HttpServlet {
    TeacherService teacherService = new TeacherService();

    protected void doPost(HttpServletRequest request, HttpServletResponse response) throws ServletException, IOException {
        response.setContentType("application/json;charset=utf-8");
        ObjectMapper objectMapper = new ObjectMapper();
        RespBean respBean = null;
        String number = request.getParameter("number");
        String name = request.getParameter("name");
        String gender = request.getParameter("sex");
        String phone = request.getParameter("phone");
        String qq = request.getParameter("qq");
        String[] courses = request.getParameterValues("course[]");
        Integer i = teacherService.addTeacher(number,name,gender,phone,qq,courses);
        if (i == 1){
            respBean= RespBean.ok("添加成功");
        }else if (i == -2){
            respBean= RespBean.ok("添加失败，教师重复添加");
        }else if (i == -1){
            respBean= RespBean.ok("添加失败");
        }
        String string = objectMapper.writeValueAsString(respBean);
        response.getWriter().write(string);

    }

    protected void doGet(HttpServletRequest request, HttpServletResponse response) throws ServletException, IOException {
        response.setContentType("application/json;charset=utf-8");
        ObjectMapper objectMapper = new ObjectMapper();
        String action = request.getParameter("action");
        if("page".equals(action)){
            request.getRequestDispatcher("WEB-INF/jsp/teacher/teacherList.jsp").forward(request, response);
        }else if ("data".equals(action)){
            String page = request.getParameter("page");
            String rows = request.getParameter("rows");
            String order = request.getParameter("order");
            String sort = request.getParameter("sort");
            RespPageBean respPageBean = teacherService.getAllTeacherByPage(page,rows,sort,order);
            String string = objectMapper.writeValueAsString(respPageBean);
            response.getWriter().write(string);
        }

    }

    @Override
    protected void doPut(HttpServletRequest req, HttpServletResponse resp) throws ServletException, IOException {
        resp.setContentType("application/json;charset=utf-8");
        ObjectMapper objectMapper = new ObjectMapper();
        RespBean respBean = null;

        Teacher teacher = objectMapper.readValue(req.getInputStream(), Teacher.class);
        Integer i =teacherService.updateTeacherCourseByNumber(teacher,teacher.getCourse());
        if (i == 1){
            respBean= RespBean.ok("更新成功");
        }else if (i == -1){
            respBean= RespBean.ok("更新失败");
        }
        String string = objectMapper.writeValueAsString(respBean);
        resp.getWriter().write(string);

    }

    @Override
    protected void doDelete(HttpServletRequest req, HttpServletResponse resp) throws ServletException, IOException {
        resp.setContentType("application/json;charset=utf-8");
        ObjectMapper objectMapper = new ObjectMapper();
        RespBean respBean = null;

        String[] tids = req.getParameterValues("tid");
        Integer i  = teacherService.deleteTeacherByTid(tids);
        if (i == 1){
            respBean= RespBean.ok("删除成功");
        }else if (i == -1){
            respBean= RespBean.ok("删除失败");
        }
        String string = objectMapper.writeValueAsString(respBean);
        resp.getWriter().write(string);

    }
}
